package com.wu.flandre.component.interceptor;

import com.wu.flandre.component.MsgRecord;
import com.wu.flandre.entity.common.ResponseTemp;
import com.wu.flandre.exceptions.UnauthorizedException;
import com.wu.flandre.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * GlobalExceptionInterceptComponent <br/>
 *
 * @author 50816 <br/>
 * @date 2019/12/11 23:29 <br/>
 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandlerInterceptor {

    @Autowired
    MsgRecord msgRecord;

    @ExceptionHandler(UnauthorizedException.class)
    public ResponseTemp accessTokenExceptionIntercept(HttpServletRequest request, HttpServletResponse response, Exception e) {
        log.error("调用Api接口发生异常,api路径:" + request.getRequestURI() + ",错误信息:" + e.getMessage());
        msgRecord.apiErrorLogRecord(e.getMessage(), request.getRequestURI());
        return ResponseUtil.unauthorized(e.getMessage());
    }

    //TODO 临时方法
    //fixme xiugaizhege
//    @ExceptionHandler(Exception.class)
//    public ResponseTemp defaultExceptionIntercept(HttpServletRequest request, HttpServletResponse response, Exception e) {
//        log.error("调用Api接口发生异常,api路径:" + request.getRequestURI() + ",错误信息:" + e.getMessage());
//        msgRecord.apiErrorLogRecord(e.getMessage(), request.getRequestURI());
//        return ResponseUtil.exceptions(e.getMessage());
//    }
}
